<template>
    <div :class="configNme+' upload_img'" v-if="configData">
        <div class="title">
            <div class="word">{{configData.title}}</div>
            <div class="bgtype">
                <Select placeholder="填充方式" v-model="configData.size" style="width:150px">
                    <Option value="fill">Full填充</Option>
                    <Option value="cover">Cover填充</Option>
                    <Option value="contain">Contain填充</Option>
                    <Option value="initial">Width填充</Option>
                </Select>
            </div>
        </div>
        <div class="box" @click="modalPicTap('单选')">
            <img :src="configData.img" v-if="configData.img">
            <div class="upload-box" v-else><Icon type="ios-camera-outline" size="36" /></div>
            <Icon @click.stop="removeImg" v-if="configData.img" type="ios-close-circle-outline" style="position:absolute;top:1%;right:1%;font-size:36px;color:#f00;"/>
        </div>
        <div>
            <Modal v-model="modalPic" width="60%" scrollable  footer-hide closable title='上传背景图' :mask-closable="false" :z-index="1">
                <uploadPictures :isChoice="isChoice" @getPic="getPic" :gridBtn="gridBtn" :gridPic="gridPic" v-if="modalPic"></uploadPictures>
            </Modal>
        </div>
    </div>
</template>

<script>
    import { mapState } from 'vuex'
    import uploadPictures from '@/components/uploadPictures';
    export default {
        name: 'c_background',
        components: {
            uploadPictures
        },
        computed: {
            ...mapState({
                tabVal: state => state.admin.mobildConfig.searchConfig.data.tabVal
            })
        },
        props: {
            configObj: {
                type: Object
            },
            configNme: {
                type: String
            }
        },
        data () {
            return {
                defaultList: [
                    {
                        'name': 'a42bdcc1178e62b4694c830f028db5c0',
                        'url': 'https://o5wwk8baw.qnssl.com/a42bdcc1178e62b4694c830f028db5c0/avatar'
                    },
                    {
                        'name': 'bc7521e033abdd1e92222d733590f104',
                        'url': 'https://o5wwk8baw.qnssl.com/bc7521e033abdd1e92222d733590f104/avatar'
                    }
                ],
                defaults: {},
                configData: {},
                modalPic: false,
                isChoice: '单选',
                gridBtn: {
                    xl: 4,
                    lg: 8,
                    md: 8,
                    sm: 8,
                    xs: 8
                },
                gridPic: {
                    xl: 6,
                    lg: 8,
                    md: 12,
                    sm: 12,
                    xs: 12
                },
                activeIndex: 0
            }
        },
        watch: {
            configObj: {
                handler (nVal, oVal) {
                    this.defaults = nVal
                    this.configData = nVal[this.configNme]
                },
                immediate: true,
                deep: true
            }
        },
        created () {
            this.defaults = this.configObj
            this.configData = this.configObj[this.configNme]
        },
        methods: {
            // 点击图文封面
            modalPicTap (title) {
                this.modalPic = true;
            },
            // 添加自定义弹窗
            addCustomDialog (editorId) {
                window.UE.registerUI('test-dialog', function (editor, uiName) {
                    let dialog = new window.UE.ui.Dialog({
                        iframeUrl: '/admin/widget.images/index.html?fodder=dialog',
                        editor: editor,
                        name: uiName,
                        title: '上传图片',
                        cssRules: 'width:1200px;height:500px;padding:20px;'
                    });
                    this.dialog = dialog;
                    // 参考上面的自定义按钮
                    var btn = new window.UE.ui.Button({
                        name: 'dialog-button',
                        title: '上传图片',
                        cssRules: `background-image: url(../../../assets/images/icons.png);background-position: -726px -77px;`,
                        onclick: function () {
                            // 渲染dialog
                            dialog.render();
                            dialog.open();
                        }
                    });

                    return btn;
                }, 37);
            },
            removeImg(){
                this.$nextTick(() => {
                    this.configData.img = '';
                })
            },
            // 获取图片信息
            getPic (pc) {
                this.$nextTick(() => {
                    this.configData.img = pc.att_dir;
                    this.modalPic = false;
                })
            }
        }
    }
</script>

<style scoped lang="stylus">
.upload_img.imgConfig .bgtype
  display none
.title
    margin-bottom: 10px;
    padding-bottom 10px
    border-bottom:1px solid rgba(0,0,0,0.05);
    font-size:12px;
    color:#999;
    display flex
    justify-content space-between
    align-items center
    .word
       font-size:14px
.bgtype
    width 60%
    display flex
    justify-content flex-end
.box
    width 100%
    position relative
    overflow hidden
    margin-bottom 20px
    img
        width 100%
        height 100%
.upload-box
    display flex
    align-items center
    justify-content center
    width 60px
    height 60px
    background #ccc
</style>
